package com.sample.base.ui.activity

import android.content.DialogInterface
import android.util.Log
import androidx.appcompat.app.AlertDialog
import com.loyal.helper.DialogHelper
import com.sample.base.R
import com.sample.base.base.BaseActivity
import com.sample.base.databinding.ActivityDialogBinding

class DialogActivity : BaseActivity() {
    private val binding by lazy { ActivityDialogBinding.inflate(layoutInflater) }

    override fun setLayoutOrView() {
        setContentView(binding.root)
    }

    override fun afterOnCreate() {
        val stringBuilder = StringBuilder()

        binding.ok.setOnClickListener {
            stringBuilder.setLength(0)
            for (i in 0..3) {
                stringBuilder.append("这是第").append(i + 1).append("句你好.\n")
            }
            stringBuilder.append("\t\t\t\t--OK测试.\n")
            DialogHelper.showInActivity(this, stringBuilder)
        }
        binding.cancel.setOnClickListener {
            stringBuilder.setLength(0)
            for (i in 0..3) {
                stringBuilder.append("这是第").append(i + 1).append("句你好.\n")
            }
            stringBuilder.append("\t\t\t\t--CANCEL测试.\n")
            val alertDialog = DialogHelper.createMessageDialog(this, stringBuilder)
            alertDialog?.setButton(
                DialogInterface.BUTTON_NEGATIVE, "取消"
            ) { dialog, _ -> dialog?.dismiss() }
            alertDialog?.show()
        }
        binding.okCancel.setOnClickListener {
            stringBuilder.setLength(0)
            for (i in 0..30) {
                stringBuilder.append("这是第------").append(i + 1).append("------句你好.\n")
            }
            stringBuilder.append("\t\t\t\t--OK/CANCEL测试.\n")

            val alertDialog = DialogHelper.createMessageDialog(this, stringBuilder)
            alertDialog?.setIcon(R.mipmap.ic_update)
            alertDialog?.setTitle("显示Icon")
            alertDialog?.setButton(
                DialogInterface.BUTTON_POSITIVE, "确 定"
            ) { dialog, _ -> dialog?.dismiss() }
            alertDialog?.setButton(
                DialogInterface.BUTTON_NEGATIVE, "取 消"
            ) { dialog, _ -> dialog?.dismiss() }
            alertDialog?.show()
        }
        binding.alertTI.setOnClickListener {
            alertTi()
        }
        binding.alert.setOnClickListener {
            val alertDialog = DialogHelper.createMessageDialog(this, "没有TITLE+ICON")
            alertDialog?.setButton(
                DialogInterface.BUTTON_NEGATIVE, "CANCEL"
            ) { dialog, _ -> dialog?.dismiss() }
            alertDialog?.setButton(
                DialogInterface.BUTTON_POSITIVE, " OK "
            ) { dialog, _ -> dialog?.dismiss() }
            alertDialog?.show()
        }
        binding.custom.setOnClickListener {
            DialogHelper.showInActivity(this, "测试")
            it.postDelayed({
                finish()
            }, 5 * 1000L)
        }
        binding.exit.setOnClickListener {
            showDialog("点击确定退出", true)
        }
    }

    private fun alertTi() {
        val key: String = DialogActivity::class.java.name
        Log.e(
            "TAG", """show: context=$key
 dialogMap size=${DialogHelper.size()}"""
        )
        var dialog: AlertDialog? = DialogHelper.get(key)
        if (null != dialog && dialog.isShowing) {
            dialog.dismiss()
        }
        val builder = AlertDialog.Builder(this, R.style.AppTheme_Dialog)
        builder.setMessage("有TITLE+ICON").setTitle("TITLE").setIcon(R.mipmap.ic_update)
        builder.setNegativeButton("CANCEL") { dialog1, _: Int -> dialog1.dismiss() }
        dialog = builder.create()
        dialog.setOnShowListener {
            DialogHelper.add(key, dialog)
        }
        dialog.setOnDismissListener { DialogHelper.remove(key) }
        dialog.show()
    }
}